# coding: UTF-8
# Filename : chain_app_api.py
# Company : imedia
# Author : zqz 
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl

from django.http import HttpResponse
import chain_app_data_model

tableName = 'chain_config'
#图片字段
image_column = ['slogan_path']

#获取sql所需参数
def find_need_param(need_param = '', filter_param = ''):
	param_array = []
	#获取表所有字段列表
	all_column_array = mysql.getColumns(tableName)
	#过滤字段为空
	if filter_param == None or '' == filter_param.strip():
		#两个参数都为空 则取所有字段
		if need_param == None or '' == need_param.strip():
			param_array = all_column_array
		else:
			param_array = str(need_param).split(',')
			for obj in param_array:
				if obj not in all_column_array:
					param_array.remove(obj)
	#过滤字段不为空
	else:
		filter_array = str(filter_param).split(',')
		for fa in filter_array:
			if fa in all_column_array:
				all_column_array.remove(str(fa))
		param_array = all_column_array
	return param_array

#动态获取模板
def get_body_model(need_param = '', filter_param = ''):
	
	param_array = find_need_param(need_param, filter_param)
	model_body_str = ''
	for column in param_array:
		temp = chain_app_data_model.line_model.replace('{line_tpl}', str(column))
		model_body_str += temp
	return chain_app_data_model.imedia_body_tpl.replace('{body}', model_body_str)

		
#details list
def chain_app_list(request):
    #record log((frist action))
    httputils.footprint(request)
    #需要过滤字段
    need_param = httputils.post(request, 'need_param')
    #不需要过滤字段
    filter_param = httputils.post(request, 'filter_param')
    #param
    cus_id = httputils.post(request, 'cus_id')#is customer id,more
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    ratio = httputils.post(request, 'ratio')
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = 1 
    #Verification
    if validate(request):
        return validate(request)
    #models
    output = ''
    output += chain_app_data_model.imedia_start 
    output += get_body_str(cus_id, size, pagenum, need_param, filter_param, ratio)
    output += chain_app_data_model.imedia_end
    return response(output)
#获取中间数据体
def get_body_str(cus_id, size, pagenum, need_param, filter_param, ratio):
	#获取动态模板
    tpl_model = get_body_model(need_param, filter_param)
    #获取所需字段
    columns = find_need_param(need_param, filter_param)
    #sql
    sql = '''
    select * from chain_config
    where cus_id=%s
    limit %s, %s
    '''
    #获取起始行
    start_row = utils.get_star_row(size, pagenum)
    param = [int(cus_id), start_row, int(size)]
    rsts = mysql.doSearch(sql, param)
    fields = mysql.searchColumnIndex(sql, param)
    body_str = ''
    for obj in rsts:
        body_template = tpl_model
        for column in columns:
        	column_value = obj[fields[column]]
        	if column in image_column:
        		column_value = image_path(column_value, ratio, False)
        	body_template = body_template.replace('{' + str(column) + '}', str(column_value))
        body_str += body_template
    return body_str



